package start.fly.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import start.fly.test.database.control_user;

public class logincheck extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session= request.getSession();
		String shenfen = request.getParameter("shenfen");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		control_user cu = new control_user();
		String checkcode=request.getParameter("checkCode");  
//		Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
//		int minute = c.get(Calendar.MINUTE);
//		//限制登录的最短时间
//		 int limitLoginTime = 0;
//		//防止用户在少于指定时间内登录
//		int i;
//		if(session.getAttribute("i")==null){
//			i=1;
//		}else{
//		i=(Integer) session.getAttribute("i");
//		}
//		System.out.println("登录了"+i+"次");
//		if(i%2!=0){
//		  session.setAttribute("first",minute);
//		  limitLoginTime=0;
//		  String logintime=(String) session.getAttribute("second");
//		  String logintime1=(String) session.getAttribute("first");
//			System.out.println("上次登录时间"+logintime);
//			System.out.println("这次登录时间"+logintime1);
//		}else{
//			session.setAttribute("second",minute);
//		//	limitLoginTime=(Integer)session.getAttribute("second")-(Integer)session.getAttribute("fisrt");
//		    System.out.println(session.getAttribute("second"));
//		    String logintime=(String) session.getAttribute("first");
//		    String logintime1=(String) session.getAttribute("second");
//			System.out.println("上次登录时间"+logintime);
//			System.out.println("这次登录时间"+logintime1);
//		}
//		i++;
//		session.setAttribute("i",i);
		
        if(checkcode.equals("")||checkcode==null){  
            out.print("<script>alert('请输入验证码');document.location='login.jsp'</script>");  
        }else{  
            if(!checkcode.equalsIgnoreCase((String)session.getAttribute("randCheckCode"))){  
                out.print("<script>alert('验证码不正确,请重新输入');document.location='login.jsp'</script>");  
            }else{  
            	System.out.println("shenfen="+shenfen);       
            	 if(cu.select_user(username,password)){
            		//用户通过验证
            		cu.update_logintime(username);
            		session.setAttribute("username",username);
            		if(cu.select_userByShenfen(username,shenfen)){
            			if(shenfen.equals("0")){
                		response.sendRedirect("user/memberIndex.jsp");
            			}else{
            			response.sendRedirect("index.jsp");	
            			}
            		}else{
            			out.print("<script>alert('非法进入');document.location='login.jsp'</script>");
            		}
            	}else{
            		out.print("<script>alert('用户名或密码错误');document.location='login.jsp'</script>");
            	}   
            	
            	
            	} 
            }
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);

	}

}
